# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.adminLogDao import AdminLogDao

from . import app

CORS(app, supports_credentials=True)
__author__ = "Prince_CHEN"


@app.route('/insertAdminLog', methods=['POST'])
def insert_admin_log():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'userId': request.json.get('userId', ''),
        'userName': request.json.get('userName', ''),
        'userIp': request.json.get('userIp', ''),
        'actionType': request.json.get('actionType', ''),
        'logRemark': request.json.get('logRemark', '')
    }

    info = AdminLogDao().insertAdminLog(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findAdminLogById', methods=['POST'])
def find_admin_log_by_id():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    logId = request.json.get('logId', '')
    info = AdminLogDao().findLogById(logId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 1, "status": "error"}})


@app.route('/findAdminLogByCondition', methods=['POST'])
def find_admin_log_by_condition():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = AdminLogDao().findLogByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                   multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 1, "status": "error"}})


@app.route('/deleteAdminLogByIds', methods=['POST'])
def delete_admin_log_by_ids():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = AdminLogDao().deleteAdminLog(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateAdminLogById', methods=['POST'])
def update_admin_log_by_id():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        logId = request.json['logId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = AdminLogDao().selectAdminLog(logId)
    info = {
        'logId': request.json['logId'],
        'userId': request.json.get('userId', oldInfo[1]),
        'userName': request.json.get('userName', oldInfo[2]),
        'userIp': request.json.get('userIp', oldInfo[3]),
        'actionType': request.json.get('actionType', oldInfo[4]),
        'logRemark': request.json.get('logRemark', oldInfo[5])
    }

    info = AdminLogDao().updateAdminLog(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
